<?php

defined('BASEPATH') or die('forbidden');

/**
 * 分页类
 * @author  Karson
 * @name    page.php
 * @since   2011-03-19 10:22:32
 */
class page {

    var $totals;   //记录总数
    var $current;    //当前分页地址
    var $pageurl;    //分页的URL
    var $pagesize;   //每页显示数
    var $item;   //分页中显示页数的个数
    var $page;   //总分页数
    var $from;   //循环开始数
    var $to;    //循环结束数
    var $info;   //分页数据
    var $show_nums; //是否显示记录总数
    var $show_page; //是否显示手动跳转页面

    function page() {
        $this->pagesize = PAGE_SIZE;
        $this->item = PAGE_ITEM;
        $this->page = 1;
        $this->from = 1;
        $this->to = 1;
        $this->info = '';
        $this->show_nums = 1;
        $this->show_page = 1;
    }

    /**
     * 获取分页数据
     * @param string $pageurl 分页的URL地址
     * @param int $totals 总记录数
     * @param int $current  当前页码
     * @param int $pagesize 分页大小
     * @param bool $show_nums   是否显示总记录数
     * @param bool $show_page   是否显示手动跳转
     * @return string 分页的数据
     */
    function get($pageurl = null, $totals = null, $current = null, $pagesize = null, $show_nums = null, $show_page = null) {
        $this->pageurl = is_null($pageurl) ? $this->pageurl : $pageurl;
        $this->totals = is_null($totals) ? $this->totals : $totals;
        $this->current = is_null($current) ? $this->current : $current;
        $this->pagesize = is_null($pagesize) ? $this->pagesize : $pagesize;
        $this->show_nums = is_null($show_nums) ? $this->show_nums : $show_nums;
        $this->show_page = is_null($show_page) ? $this->show_page : $show_page;
        $this->info = "共 {$this->totals} 条记录 ";
        //判断记录总数是否大于分页大小
        if ($this->totals > $this->pagesize) {
            if ($this->pageurl == '') {
                $this->pageurl = current_url();
                $this->pageurl .= substr($this->pageurl, -1) == '/' ? '{page}/' : '/{page}/';
            } else {
                //$this->pageurl .= substr($this->pageurl, -1) == '/' ? '' : '/';
                $this->pageurl .= ( !stripos($this->pageurl, '{page}')) ? '{page}/' : '';
            }
            $this->info = $this->show_nums ? $this->info : '';
            //获取总页数
            $this->page = ceil($this->totals / $this->pagesize);

            //当总页数小于每页显示的分页数时
            if ($this->page < $this->item)
                $this->to = $this->page;

            $this->current = $this->current > $this->page ? $this->page : $this->current;
            //分页大于每页显示的分页数 且 当前分页大于每页显示的记录数时
            if ($this->current < $this->item) {
                $this->from = 1;
            } else if ($this->current > ($this->page - $this->item + 1)) {
                $this->from = $this->page - $this->item + 1;
            } else {
                $this->from = $this->current - ceil($this->item / 2) + 1;
            }
            $this->to = $this->from + $this->item;
            $this->to = $this->to > $this->page + 1 ? $this->page + 1 : $this->to;
            $this->info .= $this->current == 1 ? "<span><<</span>" : "<a href='" . str_replace('{page}', ($this->current - 1), $this->pageurl) . "' " . $this->get_attr_result($this->current - 1) . "><<</a>";

            if ($this->current >= $this->item) {
                $this->info .= "<a href=\"" . str_replace('{page}', 1, $this->pageurl) . "\"" . $this->get_attr_result(1) . ">1</a>...";
            }
            for ($this->from; $this->from < $this->to; $this->from++) {
                $this->info .= $this->from == $this->current ? "<span class='current'>{$this->from}</span>" : "<a href=\"" . str_replace('{page}', $this->from, $this->pageurl) . "\"" . $this->get_attr_result($this->from) . ">{$this->from}</a>";
            }
            if (($this->page - $this->to) >= 1) {
                $this->info .= "...<a href=\"" . str_replace('{page}', $this->page, $this->pageurl) . "\"" . $this->get_attr_result($this->page) . ">{$this->page}</a>";
            }

            $this->info .= $this->current == $this->page ? "<span>>></span>" : "<a href=\"" . str_replace('{page}', ($this->current + 1), $this->pageurl) . "\"" . $this->get_attr_result($this->current + 1) . ">>></a>";

            $this->info .= $this->show_page ? '<form name="searchform" method="POST" action="' . str_replace('{page}', 1, $this->pageurl) . '" style="display:inline;"><input type="text" name="page" style="width:22px;text-align:center;" size="1" value="" /><input type="button" onclick="searchform.submit();" value="GO" /></form>' : '';
            return $this->info;
        } else {
            return $this->info;
        }
    }

    function show($pageurl = null, $totals = null, $current = null, $pagesize = null, $show_nums = null, $show_page = null) {
        $this->pageurl = is_null($pageurl) ? $this->pageurl : $pageurl;
        $this->totals = is_null($totals) ? $this->totals : $totals;
        $this->current = is_null($current) ? $this->current : $current;
        $this->pagesize = is_null($pagesize) ? $this->pagesize : $pagesize;
        $this->show_nums = is_null($show_nums) ? $this->show_nums : $show_nums;
        $this->show_page = is_null($show_page) ? $this->show_page : $show_page;
        $this->info = "共 {$this->totals} 条记录 ";
        //判断记录总数是否大于分页大小
        if ($this->totals > $this->pagesize) {
            if ($this->pageurl == '') {
                $this->pageurl = current_url();
                $this->pageurl .= substr($this->pageurl, -1) == '/' ? '{page}/' : '/{page}/';
            } else {
                //$this->pageurl .= substr($this->pageurl, -1) == '/' ? '' : '/';
                $this->pageurl .= ( !stripos($this->pageurl, '{page}')) ? '{page}/' : '';
            }
            $this->info = $this->show_nums ? $this->info : '';
            //获取总页数
            $this->page = ceil($this->totals / $this->pagesize);

            //当总页数小于每页显示的分页数时
            if ($this->page < $this->item)
                $this->to = $this->page;

            $this->current = $this->current > $this->page ? $this->page : $this->current;
            //分页大于每页显示的分页数 且 当前分页大于每页显示的记录数时
            if ($this->current < $this->item) {
                $this->from = 1;
            } else if ($this->current > ($this->page - $this->item + 1)) {
                $this->from = $this->page - $this->item + 1;
            } else {
                $this->from = $this->current - ceil($this->item / 2) + 1;
            }
            $this->to = $this->from + $this->item;
            $this->to = $this->to > $this->page + 1 ? $this->page + 1 : $this->to;
            $this->info .= $this->current == 1 ? "<li class='disabled'>&#171; 上一页</li>" : "<li><a href='" . str_replace('{page}', ($this->current - 1), $this->pageurl) . "' " . $this->get_attr_result($this->current - 1) . "><<</a></li>";

            if ($this->current >= $this->item) {
                $this->info .= "<li><a href=\"" . str_replace('{page}', 1, $this->pageurl) . "\"" . $this->get_attr_result(1) . ">1</a></li><li>...</li>";
            }
            for ($this->from; $this->from < $this->to; $this->from++) {
                $this->info .= $this->from == $this->current ? "<li class='active'>{$this->from}</li>" : "<li><a href=\"" . str_replace('{page}', $this->from, $this->pageurl) . "\"" . $this->get_attr_result($this->from) . ">{$this->from}</a></li>";
            }
            if (($this->page - $this->to) >= 1) {
                $this->info .= "<li>...</li><li><a href=\"" . str_replace('{page}', $this->page, $this->pageurl) . "\"" . $this->get_attr_result($this->page) . ">{$this->page}</a></li>";
            }

            $this->info .= $this->current == $this->page ? "<li class='disabled'>>></li>" : "<li><a href=\"" . str_replace('{page}', ($this->current + 1), $this->pageurl) . "\"" . $this->get_attr_result($this->current + 1) . ">>></a></li>";

            $this->info .= $this->show_page ? '<form name="searchform" method="POST" action="' . str_replace('{page}', 1, $this->pageurl) . '" style="display:inline;"><input type="text" name="page" style="width:22px;text-align:center;" size="1" value="" /><input type="button" onclick="searchform.submit();" value="GO" /></form>' : '';
            return $this->info;
        } else {
            return $this->info;
        }
    }

    function get_attr_result($page = 1) {
        return " totals='{$this->totals}' pagesize='{$this->pagesize}' page='{$page}'";
    }

}